using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class ProductDAL
    {
        public static int Save(ProductInfo info)
        {
            string sql = string.Format("insert into [product]([AdminID],[Category1ID],[Category2ID],[Num],[Name],[Imgs],[OPrice],[APrice],[Unit],[Tags],[SellCount],[Context],[ClickCount],[Sort],[CreateDate]) values(@AdminID,@Category1ID,@Category2ID,@Num,@Name,@Imgs,@OPrice,@APrice,@Unit,@Tags,@SellCount,@Context,@ClickCount,@Sort,@CreateDate);select @@identity;");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Category1ID", info.Category1ID),
                new SqlParameter("@Category2ID", info.Category2ID),
                new SqlParameter("@Num", info.Num),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Imgs", info.Img),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@Tags", info.Tags),
                new SqlParameter("@SellCount", info.SellCount),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@ClickCount", info.ClickCount),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@CreateDate", info.CreateDate)
            };
            return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
        }
        public static int Update(ProductInfo info)
        {
            string sql = string.Format("update [product] set [AdminID]=@AdminID,[Category1ID]=@Category1ID,[Category2ID]=@Category2ID,[Num]=@Num,[Name]=@Name,[Imgs]=@Imgs,[OPrice]=@OPrice,[APrice]=@APrice,[Unit]=@Unit,[Tags]=@Tags,[SellCount]=@SellCount,[Context]=@Context,[Sort]=@Sort where [ID]=@ID");
            SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@AdminID", info.AdminID),
                new SqlParameter("@Category1ID", info.Category1ID),
                new SqlParameter("@Category2ID", info.Category2ID),
                new SqlParameter("@Num", info.Num),
                new SqlParameter("@Name", info.Name),
                new SqlParameter("@Imgs", info.Img),
                new SqlParameter("@OPrice", info.OPrice),
                new SqlParameter("@APrice", info.APrice),
                new SqlParameter("@Unit", info.Unit),
                new SqlParameter("@Tags", info.Tags),
                new SqlParameter("@SellCount", info.SellCount),
                new SqlParameter("@Context", info.Context),
                new SqlParameter("@Sort", info.Sort),
                new SqlParameter("@ID", info.ID)
            };
            return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
        }
        public static ProductInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[product]");
            return SimpleData(dr);
        }
        public static List<ProductInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[product]");
            return ListSimpleData(dt);
        }
        public static bool IncClickCount(int id)
        {
            return CommonDAL.ExecuteNoneQuery(string.Format("update [product] set ClickCount=ClickCount+1 where ID={0}", id));
        }
        public static List<ProductInfo> FindPage(int category1id, int category2id, string keyword, int adminid, string begindate, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            filter.AppendFormat(" and [product].AdminID={0}", adminid);
            if (category1id != 0)
            {
                filter.AppendFormat(" and [product].Category1ID ={0}", category1id);
            }
            if (category2id != 0)
            {
                filter.AppendFormat(" and [product].Category2ID ={0}", category2id);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [product].Name like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(begindate) && !string.IsNullOrEmpty(enddate))
            {
                filter.AppendFormat(" and [product].CreateDate between '{0}' and '{1}'", DataConvert.ToDateTime(begindate).ToString("yyyy-MM-dd 00:00:00"), DataConvert.ToDateTime(enddate).ToString("yyyy-MM-dd 23:59:59"));
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[product]", out count);
            return ListSimpleData(dt);
        }
        public static List<ProductInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [product]"));
            return ListSimpleData(dt);
        }
        public static List<ProductInfo> FindTop(int adminid, int top, string tags)
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select top {0} * from [product] where [product].AdminID={1} and ','+[product].Tags+',' like '%,{2},%' order by Sort",top,adminid,tags));
            return ListSimpleData(dt);
        }
        private static ProductInfo SimpleData(DataRow dr)
        {
            ProductInfo info = new ProductInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Category1ID = DataConvert.ToInt(dr["Category1ID"]);
                info.Category2ID = DataConvert.ToInt(dr["Category2ID"]);
                info.Num = DataConvert.ToString(dr["Num"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Imgs = (Commons.domain + DataConvert.ToString(dr["Imgs"]).Replace(",",","+Commons.domain)).Split(',');
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Tags = DataConvert.ToString(dr["Tags"]);
                info.TagsList = info.Tags.Split(',');
                info.SellCount = DataConvert.ToInt(dr["SellCount"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Img = info.Imgs[0];
                info.OPriceText = info.OPrice.ToString("0.00");
                info.APriceText = info.APrice.ToString("0.00").Split('.');
            }
            return info;
        }
        private static ProductInfo AllData(DataRow dr)
        {
            ProductInfo info = new ProductInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.AdminID = DataConvert.ToInt(dr["AdminID"]);
                info.Category1ID = DataConvert.ToInt(dr["Category1ID"]);
                info.Category2ID = DataConvert.ToInt(dr["Category2ID"]);
                info.Num = DataConvert.ToString(dr["Num"]);
                info.Name = DataConvert.ToString(dr["Name"]);
                info.Imgs = (Commons.domain + DataConvert.ToString(dr["Imgs"]).Replace(",", Commons.domain)).Split(',');
                info.OPrice = DataConvert.ToFloat(dr["OPrice"]);
                info.APrice = DataConvert.ToFloat(dr["APrice"]);
                info.Unit = DataConvert.ToString(dr["Unit"]);
                info.Tags = DataConvert.ToString(dr["Tags"]);
                info.TagsList = info.Tags.Split(',');
                info.SellCount = DataConvert.ToInt(dr["SellCount"]);
                info.Context = DataConvert.ToString(dr["Context"]);
                info.ClickCount = DataConvert.ToInt(dr["ClickCount"]);
                info.Sort = DataConvert.ToInt(dr["Sort"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);

                info.Img = info.Imgs[0];
                info.OPriceText = info.OPrice.ToString("0.00");
                info.APriceText = info.APrice.ToString("0.00").Split('.');
            }
            return info;
        }
        private static List<ProductInfo> ListSimpleData(DataTable dt)
        {
            List<ProductInfo> list = new List<ProductInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(SimpleData(row));
            }
            return list;
        }
        private static List<ProductInfo> ListAllData(DataTable dt)
        {
            List<ProductInfo> list = new List<ProductInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(AllData(row));
            }
            return list;
        }
    }
}

